﻿/**  版本信息模板在安装目录下，可自行修改。
* Members.cs
*
* 功 能： N/A
* 类 名： Members
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2016/8/17 23:19:29   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:Members
	/// </summary>
	public partial class Members
	{
		public Members()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("UserId", "Members"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int UserId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Members");
			strSql.Append(" where UserId=@UserId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4)
			};
			parameters[0].Value = UserId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.Members model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Members(");
			strSql.Append("UserName,Password,GroupId,LevelId,NickName,RealName,Email,Mobile,Phone,Birthday,Sex,LocationId,PostCode,Address,Question,Answer,QQ,MSN,Weixin,IdentityType,IdentityCard,Credits,Pic,Status,CreateTime,UpdateTime,LastLoginTime,IsActive,IdentityCardPic1,IdentityCardPic2)");
			strSql.Append(" values (");
			strSql.Append("@UserName,@Password,@GroupId,@LevelId,@NickName,@RealName,@Email,@Mobile,@Phone,@Birthday,@Sex,@LocationId,@PostCode,@Address,@Question,@Answer,@QQ,@MSN,@Weixin,@IdentityType,@IdentityCard,@Credits,@Pic,@Status,@CreateTime,@UpdateTime,@LastLoginTime,@IsActive,@IdentityCardPic1,@IdentityCardPic2)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@GroupId", SqlDbType.Int,4),
					new SqlParameter("@LevelId", SqlDbType.Int,4),
					new SqlParameter("@NickName", SqlDbType.NVarChar,50),
					new SqlParameter("@RealName", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Birthday", SqlDbType.VarChar,20),
					new SqlParameter("@Sex", SqlDbType.VarChar,10),
					new SqlParameter("@LocationId", SqlDbType.Int,4),
					new SqlParameter("@PostCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Question", SqlDbType.NVarChar,50),
					new SqlParameter("@Answer", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,50),
					new SqlParameter("@MSN", SqlDbType.NVarChar,50),
					new SqlParameter("@Weixin", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityType", SqlDbType.Int,4),
					new SqlParameter("@IdentityCard", SqlDbType.VarChar,50),
					new SqlParameter("@Credits", SqlDbType.Int,4),
					new SqlParameter("@Pic", SqlDbType.NVarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@LastLoginTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@IdentityCardPic1", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityCardPic2", SqlDbType.VarChar,50)};
			parameters[0].Value = model.UserName;
			parameters[1].Value = model.Password;
			parameters[2].Value = model.GroupId;
			parameters[3].Value = model.LevelId;
			parameters[4].Value = model.NickName;
			parameters[5].Value = model.RealName;
			parameters[6].Value = model.Email;
			parameters[7].Value = model.Mobile;
			parameters[8].Value = model.Phone;
			parameters[9].Value = model.Birthday;
			parameters[10].Value = model.Sex;
			parameters[11].Value = model.LocationId;
			parameters[12].Value = model.PostCode;
			parameters[13].Value = model.Address;
			parameters[14].Value = model.Question;
			parameters[15].Value = model.Answer;
			parameters[16].Value = model.QQ;
			parameters[17].Value = model.MSN;
			parameters[18].Value = model.Weixin;
			parameters[19].Value = model.IdentityType;
			parameters[20].Value = model.IdentityCard;
			parameters[21].Value = model.Credits;
			parameters[22].Value = model.Pic;
			parameters[23].Value = model.Status;
			parameters[24].Value = model.CreateTime;
			parameters[25].Value = model.UpdateTime;
			parameters[26].Value = model.LastLoginTime;
			parameters[27].Value = model.IsActive;
			parameters[28].Value = model.IdentityCardPic1;
			parameters[29].Value = model.IdentityCardPic2;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.Members model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Members set ");
			strSql.Append("UserName=@UserName,");
			strSql.Append("Password=@Password,");
			strSql.Append("GroupId=@GroupId,");
			strSql.Append("LevelId=@LevelId,");
			strSql.Append("NickName=@NickName,");
			strSql.Append("RealName=@RealName,");
			strSql.Append("Email=@Email,");
			strSql.Append("Mobile=@Mobile,");
			strSql.Append("Phone=@Phone,");
			strSql.Append("Birthday=@Birthday,");
			strSql.Append("Sex=@Sex,");
			strSql.Append("LocationId=@LocationId,");
			strSql.Append("PostCode=@PostCode,");
			strSql.Append("Address=@Address,");
			strSql.Append("Question=@Question,");
			strSql.Append("Answer=@Answer,");
			strSql.Append("QQ=@QQ,");
			strSql.Append("MSN=@MSN,");
			strSql.Append("Weixin=@Weixin,");
			strSql.Append("IdentityType=@IdentityType,");
			strSql.Append("IdentityCard=@IdentityCard,");
			strSql.Append("Credits=@Credits,");
			strSql.Append("Pic=@Pic,");
			strSql.Append("Status=@Status,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("UpdateTime=@UpdateTime,");
			strSql.Append("LastLoginTime=@LastLoginTime,");
			strSql.Append("IsActive=@IsActive,");
			strSql.Append("IdentityCardPic1=@IdentityCardPic1,");
			strSql.Append("IdentityCardPic2=@IdentityCardPic2");
			strSql.Append(" where UserId=@UserId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@GroupId", SqlDbType.Int,4),
					new SqlParameter("@LevelId", SqlDbType.Int,4),
					new SqlParameter("@NickName", SqlDbType.NVarChar,50),
					new SqlParameter("@RealName", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Birthday", SqlDbType.VarChar,20),
					new SqlParameter("@Sex", SqlDbType.VarChar,10),
					new SqlParameter("@LocationId", SqlDbType.Int,4),
					new SqlParameter("@PostCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Question", SqlDbType.NVarChar,50),
					new SqlParameter("@Answer", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,50),
					new SqlParameter("@MSN", SqlDbType.NVarChar,50),
					new SqlParameter("@Weixin", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityType", SqlDbType.Int,4),
					new SqlParameter("@IdentityCard", SqlDbType.VarChar,50),
					new SqlParameter("@Credits", SqlDbType.Int,4),
					new SqlParameter("@Pic", SqlDbType.NVarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@LastLoginTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@IdentityCardPic1", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityCardPic2", SqlDbType.VarChar,50),
					new SqlParameter("@UserId", SqlDbType.Int,4)};
			parameters[0].Value = model.UserName;
			parameters[1].Value = model.Password;
			parameters[2].Value = model.GroupId;
			parameters[3].Value = model.LevelId;
			parameters[4].Value = model.NickName;
			parameters[5].Value = model.RealName;
			parameters[6].Value = model.Email;
			parameters[7].Value = model.Mobile;
			parameters[8].Value = model.Phone;
			parameters[9].Value = model.Birthday;
			parameters[10].Value = model.Sex;
			parameters[11].Value = model.LocationId;
			parameters[12].Value = model.PostCode;
			parameters[13].Value = model.Address;
			parameters[14].Value = model.Question;
			parameters[15].Value = model.Answer;
			parameters[16].Value = model.QQ;
			parameters[17].Value = model.MSN;
			parameters[18].Value = model.Weixin;
			parameters[19].Value = model.IdentityType;
			parameters[20].Value = model.IdentityCard;
			parameters[21].Value = model.Credits;
			parameters[22].Value = model.Pic;
			parameters[23].Value = model.Status;
			parameters[24].Value = model.CreateTime;
			parameters[25].Value = model.UpdateTime;
			parameters[26].Value = model.LastLoginTime;
			parameters[27].Value = model.IsActive;
			parameters[28].Value = model.IdentityCardPic1;
			parameters[29].Value = model.IdentityCardPic2;
			parameters[30].Value = model.UserId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int UserId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Members ");
			strSql.Append(" where UserId=@UserId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4)
			};
			parameters[0].Value = UserId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string UserIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Members ");
			strSql.Append(" where UserId in ("+UserIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Members GetModel(int UserId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 UserId,UserName,Password,GroupId,LevelId,NickName,RealName,Email,Mobile,Phone,Birthday,Sex,LocationId,PostCode,Address,Question,Answer,QQ,MSN,Weixin,IdentityType,IdentityCard,Credits,Pic,Status,CreateTime,UpdateTime,LastLoginTime,IsActive,IdentityCardPic1,IdentityCardPic2 from Members ");
			strSql.Append(" where UserId=@UserId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4)
			};
			parameters[0].Value = UserId;

			Maticsoft.Model.Members model=new Maticsoft.Model.Members();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Members DataRowToModel(DataRow row)
		{
			Maticsoft.Model.Members model=new Maticsoft.Model.Members();
			if (row != null)
			{
				if(row["UserId"]!=null && row["UserId"].ToString()!="")
				{
					model.UserId=int.Parse(row["UserId"].ToString());
				}
				if(row["UserName"]!=null)
				{
					model.UserName=row["UserName"].ToString();
				}
				if(row["Password"]!=null)
				{
					model.Password=row["Password"].ToString();
				}
				if(row["GroupId"]!=null && row["GroupId"].ToString()!="")
				{
					model.GroupId=int.Parse(row["GroupId"].ToString());
				}
				if(row["LevelId"]!=null && row["LevelId"].ToString()!="")
				{
					model.LevelId=int.Parse(row["LevelId"].ToString());
				}
				if(row["NickName"]!=null)
				{
					model.NickName=row["NickName"].ToString();
				}
				if(row["RealName"]!=null)
				{
					model.RealName=row["RealName"].ToString();
				}
				if(row["Email"]!=null)
				{
					model.Email=row["Email"].ToString();
				}
				if(row["Mobile"]!=null)
				{
					model.Mobile=row["Mobile"].ToString();
				}
				if(row["Phone"]!=null)
				{
					model.Phone=row["Phone"].ToString();
				}
				if(row["Birthday"]!=null)
				{
					model.Birthday=row["Birthday"].ToString();
				}
				if(row["Sex"]!=null)
				{
					model.Sex=row["Sex"].ToString();
				}
				if(row["LocationId"]!=null && row["LocationId"].ToString()!="")
				{
					model.LocationId=int.Parse(row["LocationId"].ToString());
				}
				if(row["PostCode"]!=null)
				{
					model.PostCode=row["PostCode"].ToString();
				}
				if(row["Address"]!=null)
				{
					model.Address=row["Address"].ToString();
				}
				if(row["Question"]!=null)
				{
					model.Question=row["Question"].ToString();
				}
				if(row["Answer"]!=null)
				{
					model.Answer=row["Answer"].ToString();
				}
				if(row["QQ"]!=null)
				{
					model.QQ=row["QQ"].ToString();
				}
				if(row["MSN"]!=null)
				{
					model.MSN=row["MSN"].ToString();
				}
				if(row["Weixin"]!=null)
				{
					model.Weixin=row["Weixin"].ToString();
				}
				if(row["IdentityType"]!=null && row["IdentityType"].ToString()!="")
				{
					model.IdentityType=int.Parse(row["IdentityType"].ToString());
				}
				if(row["IdentityCard"]!=null)
				{
					model.IdentityCard=row["IdentityCard"].ToString();
				}
				if(row["Credits"]!=null && row["Credits"].ToString()!="")
				{
					model.Credits=int.Parse(row["Credits"].ToString());
				}
				if(row["Pic"]!=null)
				{
					model.Pic=row["Pic"].ToString();
				}
				if(row["Status"]!=null && row["Status"].ToString()!="")
				{
					model.Status=int.Parse(row["Status"].ToString());
				}
				if(row["CreateTime"]!=null && row["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(row["CreateTime"].ToString());
				}
				if(row["UpdateTime"]!=null && row["UpdateTime"].ToString()!="")
				{
					model.UpdateTime=DateTime.Parse(row["UpdateTime"].ToString());
				}
				if(row["LastLoginTime"]!=null && row["LastLoginTime"].ToString()!="")
				{
					model.LastLoginTime=DateTime.Parse(row["LastLoginTime"].ToString());
				}
				if(row["IsActive"]!=null && row["IsActive"].ToString()!="")
				{
					model.IsActive=int.Parse(row["IsActive"].ToString());
				}
				if(row["IdentityCardPic1"]!=null)
				{
					model.IdentityCardPic1=row["IdentityCardPic1"].ToString();
				}
				if(row["IdentityCardPic2"]!=null)
				{
					model.IdentityCardPic2=row["IdentityCardPic2"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserId,UserName,Password,GroupId,LevelId,NickName,RealName,Email,Mobile,Phone,Birthday,Sex,LocationId,PostCode,Address,Question,Answer,QQ,MSN,Weixin,IdentityType,IdentityCard,Credits,Pic,Status,CreateTime,UpdateTime,LastLoginTime,IsActive,IdentityCardPic1,IdentityCardPic2 ");
			strSql.Append(" FROM Members ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" UserId,UserName,Password,GroupId,LevelId,NickName,RealName,Email,Mobile,Phone,Birthday,Sex,LocationId,PostCode,Address,Question,Answer,QQ,MSN,Weixin,IdentityType,IdentityCard,Credits,Pic,Status,CreateTime,UpdateTime,LastLoginTime,IsActive,IdentityCardPic1,IdentityCardPic2 ");
			strSql.Append(" FROM Members ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Members ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.UserId desc");
			}
			strSql.Append(")AS Row, T.*  from Members T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Members";
			parameters[1].Value = "UserId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

